import { Component, _decorator } from "cc";
import { UIDevice } from '../UIDevice';
const { ccclass, property } = _decorator;

interface IObject {
    text: string;
    icon: string;

    selectedTitle?: string;
    selectedIcon?: string;
}

@ccclass('LocalizedGObject')
export class LocalizedGObject extends Component {
    private _obj: IObject;
    private _text: string;
    private _icon: string;
    private _selectedTitle: string;
    private _selectedIcon: string;

    public set textKey(value: string) {
        this._text = value;
        if (value == null && this._obj) {
            this._obj.text = null;
        } else {
            this.updateText();
        }
    }

    public get textKey(): string {
        return this._text;
    }

    public set iconKey(value: string) {
        this._icon = value;
        if (value == null && this._obj) {
            this._obj.icon = null;
        } else {
            this.updateIcon();
        }
    }

    public get iconKey(): string {
        return this._icon;
    }

    public set selectedTitleKey(value: string) {
        this._selectedTitle = value;
        if (value == null && this._obj) {
            this._obj.selectedTitle = null;
        } else {
            this.updateSelectedTitle();
        }
    }

    public get selectedTitleKey(): string {
        return this._selectedTitle;
    }

    public set selectedIconKey(value: string) {
        this._selectedIcon = value;
        if (value == null && this._obj) {
            this._obj.selectedIcon = null;
        } else {
            this.updateSelectedIcon();
        }
    }

    public get selectedIconKey(): string {
        return this._selectedIcon;
    }

    init(obj: IObject): void {
        this._obj = obj;
    }

    protected onEnable(): void {
        this.updateLanguage();
    }

    public updateLanguage(): void {
        this.updateText();
        this.updateIcon();
        this.updateSelectedTitle();
        this.updateSelectedIcon();
    }

    private updateText(): void {
        if (!this._obj || this._text == null) return;
        this._obj.text = UIDevice.inst.i18n.t(this._text);
    }

    private updateIcon(): void {
        if (!this._obj || this._icon == null) return;
        this._obj.icon = UIDevice.inst.i18n.t(this._icon);
    }

    private updateSelectedTitle(): void {
        if (!this._obj || this._selectedTitle == null) return;
        this._obj.selectedTitle = UIDevice.inst.i18n.t(this._selectedTitle);
    }

    private updateSelectedIcon(): void {
        if (!this._obj || this._selectedIcon == null) return;
        this._obj.selectedIcon = UIDevice.inst.i18n.t(this._selectedIcon);
    }
}